1
|
|
|
import React from "react" |
2
|
|
|
import expectRender from "../expect-to-same-render" |
3
|
|
|
import type { ClassNames, ClassNamesProperty, ClassHash } from "." |
4
|
|
|
import classNaming, {classNamesCheck} from "." |
5
|
|
|
|
6
|
|
|
function Button({className, "classnames": { Btn }}: ClassNames<true, ClassNamesProperty<{Btn: ClassHash}>>) { |
7
|
|
|
return <button {...classNaming(className, { Btn })}/> |
8
|
|
|
} |
9
|
|
|
|
10
|
|
|
function Root({ |
11
|
|
|
classnames, "classnames": { App__Item, App__Footer } |
12
|
|
|
}: ClassNames<ClassNamesProperty<{App__Item: ClassHash; App__Footer: ClassHash}>, typeof Button>) { |
13
|
|
|
return <> |
14
|
|
|
<Button {...{ |
15
|
|
|
...classNaming({ App__Item }), |
16
|
|
|
classnames |
17
|
|
|
}}/> |
18
|
|
|
<div |
19
|
|
|
className={`${classNaming({App__Footer})}`} |
20
|
|
|
data-classname={`${classNaming({App__Footer})}`} |
21
|
|
|
/> |
22
|
|
|
</> |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
it("not css module", () => expectRender( |
26
|
|
|
<Root classnames={classNamesCheck()}/> |
27
|
|
|
).toSame( |
28
|
|
|
<button className="App__Item Btn"></button>, |
29
|
|
|
<div className="App__Footer" data-classname="App__Footer"></div> |
30
|
|
|
)) |
31
|
|
|
|
32
|
|
|
it("css module", () => expectRender( |
33
|
|
|
<Root classnames={{ |
34
|
|
|
App__Footer: "footer-hash", |
35
|
|
|
App__Item: "item-hash", |
36
|
|
|
Btn: "btn-hash" |
37
|
|
|
}}/> |
38
|
|
|
).toSame( |
39
|
|
|
<button className="item-hash btn-hash"></button>, |
40
|
|
|
<div className="footer-hash" data-classname="footer-hash"></div> |
41
|
|
|
)) |
42
|
|
|
|
43
|
|
|
it("vscode couldn't rename enum element", () => { |
44
|
|
|
function Root({ |
45
|
|
|
"classnames": {App: App__Container} |
46
|
|
|
}: ClassNames<ClassNamesProperty<{App: ClassHash}>>) { |
47
|
|
|
return <div {...classNaming({App: App__Container})}/> |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
expectRender( |
51
|
|
|
<Root classnames={classNamesCheck()} /> |
52
|
|
|
).toSame( |
53
|
|
|
<div className="App"></div> |
54
|
|
|
) |
55
|
|
|
}) |
56
|
|
|
|
57
|
|
|
it("vscode can rename property", () => { |
58
|
|
|
type RootProps = { |
59
|
|
|
classnames: { |
60
|
|
|
App__Container: ClassHash |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
function Root({ |
65
|
|
|
"classnames": {App__Container: App__Container} |
66
|
|
|
}: RootProps) { |
67
|
|
|
return <div {...classNaming({App: App__Container})}/> |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
function Root2({ |
71
|
|
|
"classnames": { |
72
|
|
|
// VSCode didn't rename here due to `ClassNames<>` wrapper |
73
|
|
|
//@ts-expect-error Property 'App' does not exist |
74
|
|
|
App: App__Container |
75
|
|
|
} |
76
|
|
|
}: ClassNames<typeof Root>) { |
77
|
|
|
return <div {...classNaming({App: App__Container})}/> |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
expectRender( |
81
|
|
|
<Root |
82
|
|
|
classnames={ |
83
|
|
|
classNamesCheck()} />, |
84
|
|
|
<Root2 classnames={classNamesCheck()} /> |
85
|
|
|
).toSame( |
86
|
|
|
<div className="App"/>, |
87
|
|
|
<div className="App"/> |
88
|
|
|
) |
89
|
|
|
}) |
90
|
|
|
|
91
|
|
|
it("additional type check after rename", () => { |
92
|
|
|
type Props1 = { |
93
|
|
|
classnames: { class1: ClassHash } |
94
|
|
|
} |
95
|
|
|
type Props2 = { |
96
|
|
|
classnames: { class2_renamed: ClassHash } |
97
|
|
|
} |
98
|
|
|
const { class1, |
99
|
|
|
//@ts-expect-error Property 'class2' does not exist |
100
|
|
|
class2 |
101
|
|
|
} = classNamesCheck<Props1 & Props2>(); |
102
|
|
|
|
103
|
|
|
expectRender( |
104
|
|
|
<div {...classNaming<Props1["classnames"]>({class1})} />, |
105
|
|
|
<div {...classNaming<Props2["classnames"]>({ |
106
|
|
|
//@ts-expect-error Object literal may only specify known properties, and 'class2' does not exist |
107
|
|
|
class2 |
108
|
|
|
})} /> |
109
|
|
|
).toSame( |
110
|
|
|
<div className="class1"/>, |
111
|
|
|
<div className="class2"/> |
112
|
|
|
) |
113
|
|
|
}) |
114
|
|
|
|
115
|
|
|
it("chaining", () => { |
116
|
|
|
const props = {className: "Cell", classnames: classNamesCheck()} |
117
|
|
|
|
118
|
|
|
const {className, classnames: { |
119
|
|
|
Column_1, Column_2, |
120
|
|
|
Row_1, Row_2 |
121
|
|
|
}} = props |
122
|
|
|
, cn1 = classNaming(className, {Column_1}) |
123
|
|
|
, cn2 = classNaming(className, {Column_2}) |
124
|
|
|
|
125
|
|
|
expectRender( |
126
|
|
|
<div {...cn1({ Row_1 })} />, |
127
|
|
|
<div {...cn1({ Row_2 })} />, |
128
|
|
|
<div {...cn2({ Row_1 })} />, |
129
|
|
|
<div {...cn2({ Row_2 })} />, |
130
|
|
|
<div {...classNaming({ Column_1 })({ Column_2 })({ Row_1 })({ Row_2 })} /> |
131
|
|
|
).toSame( |
132
|
|
|
<div className="Cell Column_1 Row_1" />, |
133
|
|
|
<div className="Cell Column_1 Row_2" />, |
134
|
|
|
<div className="Cell Column_2 Row_1" />, |
135
|
|
|
<div className="Cell Column_2 Row_2" />, |
136
|
|
|
<div className="Column_1 Column_2 Row_1 Row_2" /> |
137
|
|
|
) |
138
|
|
|
}) |
139
|
|
|
|